import requests
from DrissionPage import Chromium

tab = Chromium().latest_tab

tab.listen.start("/aweme/v1/web/aweme/post/")
tab.get(
    "https://www.douyin.com/user/MS4wLjABAAAAiTdU1xNUAJkeaGuaRuhNubR-46mA6YwkBolYgbt5K2bJ_JRDOXz1KM2MC9tap9WK?from_tab_name=main")
res = tab.listen.wait()
data = res.response.body
print(data)

count = 0
headers = {
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36",
    "referer": "https://www.douyin.com/"
}

for item in data['aweme_list']:
    url = item['video']['play_addr']['url_list'][0]
    print(url)
    res = requests.get(url, headers=headers)

    with open(f"./media/{count}.{'mp3' if url.find('.mp3') >= 0 else 'mp4'}", "wb") as f:
        f.write(res.content)
    count += 1

tab.listen.wait()
